﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FancyIdea.PingShui.Core.Dal;
using System.Web.UI.HtmlControls;

namespace FancyIdea.PingShui.view
{
    public partial class 打印 : System.Web.UI.Page
    {
        string 纳税人代码 = null;
        DateTime 操作时间 = DateTime.MinValue;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                纳税人代码 = Request["纳税人代码"].ToString();
                操作时间 = DateTime.Parse(Request["操作时间"].ToString());
            }
            catch
            {
                //丢失
            }
            LoadData();
            LoadProjectData();
        }

        private void LoadData()
        {
            if (string.IsNullOrEmpty(纳税人代码))
            {
                return;
            }
            DataTable table = 评税信息操作.GetPrintData(纳税人代码, 操作时间);
            if (table != null && table.Rows.Count > 0)
            {
                try
                {
                    lblName.Text = table.Rows[0]["纳税人名称"].ToString();
                    lblMoney.Text = table.Rows[0]["执行定额"].ToString();
                    DateTime start = DateTime.Parse(table.Rows[0]["定额执行日期"].ToString());
                    DateTime end = DateTime.Parse(table.Rows[0]["定额截止日期"].ToString());
                    lblStartYear.Text = start.Year.ToString();
                    lblStartMonth.Text = start.Month.ToString();
                    lblStartDay.Text = start.Day.ToString();
                    lblEndYear.Text = end.Year.ToString();
                    lblEndMonth.Text = end.Month.ToString();
                    lblEndDay.Text = end.Day.ToString();
                    LoadTable(table);
                }
                catch
                {
                    //丢失
                }
            }
        }

        private void LoadProjectData()
        {
            DataTable project = 征收项目品目操作.GetProject();
            foreach (DataRow row in project.Rows)
            {
                HtmlGenericControl span = new HtmlGenericControl("span");
                span.InnerText = row["征收项目名称"].ToString();
                span.Attributes.Add("code", row["征收项目代码"].ToString());
                this.project.Controls.Add(span);
            }

            DataTable item = 征收项目品目操作.GetItems();
            foreach (DataRow row in item.Rows)
            {
                HtmlGenericControl span = new HtmlGenericControl("span");
                span.InnerText = row["征收品目名称"].ToString();
                span.Attributes.Add("code", row["征收品目代码"].ToString());
                span.Attributes.Add("project_code", row["征收项目代码"].ToString());
                span.Attributes.Add("zssl", row["征收税率"].ToString());
                this.item.Controls.Add(span);
            }
        }

        private void LoadTable(DataTable table)
        {
            TableCell td;
            TableRow tr;
            for (int i = 0; i < 8; i++)
            {
                tr = new TableRow();
                for (int j = 0; j < 4; j++)
                {
                    td = new TableCell();
                    td.Controls.Add(new LiteralControl(""));
                    tr.Cells.Add(td);
                }
                ContentTable.Rows.Add(tr);
            }
            int index = 0;
            foreach (DataRow row in table.Rows)
            {
                ContentTable.Rows[index].Cells[0].Text = row["征收品目名称"].ToString();
                string 执行定额 = row["执行定额"].ToString();
                ContentTable.Rows[index].Cells[1].Text = 执行定额;
                string 征收税率 = row["征收税率"].ToString();
                ContentTable.Rows[index].Cells[2].Text = 征收税率;
                try
                {
                    ContentTable.Rows[index].Cells[3].Text =
                        (float.Parse(执行定额) * float.Parse(征收税率) / 100).ToString("0.0");
                }
                catch { }
                index++;
            }
            tr = new TableRow();
            for (int j = 0; j < 2; j++)
            {
                td = new TableCell();
                td.Controls.Add(new LiteralControl(""));
                tr.Cells.Add(td);
            }
            ContentTable.Rows.Add(tr);
        }
    }
}